<template>
  <div class="app-container">
    <el-tabs v-model="listQuery.status" type="card" style="margin-bottom:10px">
      <el-tab-pane v-for="item in jcStatusJson" :key="item.value" :label="item.label" :name="item.value+''">
      </el-tab-pane>
    </el-tabs>
    <div class="filter-container">
      <el-input v-model="listQuery.identifier" placeholder="竞价编号" class="filter-item" clearable/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter"
        >
        查询
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate"
        >
        竞价发布
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;">
      <el-table-column label="序号" type="index" align="center" width="60px" />
      <el-table-column label="竞价编号" prop="identifier" align="center" />
      <el-table-column label="产品名称" prop="name" align="center" />
      <el-table-column label="分类名称" prop="categoryName" align="center" />
      <el-table-column label="单位" prop="unit" align="center" />
      <el-table-column label="起拍价" prop="topPrice" align="center" />
      <el-table-column label="竞价类型" prop="typeText" align="center" />
      <!-- <el-table-column label="竞价类型" align="center">
        <template slot-scope="{row}">
          {{row.type === 0 ? '依次递减' : row.type === 1 ? '依次递增' : ''}}
        </template>
      </el-table-column> -->
      <el-table-column label="竞拍数量" prop="topNum" align="center" />
      <el-table-column label="竞价时间" align="center">
        <template slot-scope="{row}">
          {{row.startDate}}至{{row.endDate}}
        </template>
      </el-table-column>
      <el-table-column label="最新价格" prop="newPrice" align="center" />
      <el-table-column label="出价次数" prop="priceTimes" align="center" />
      <el-table-column label="状态" prop="statusText" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.statusText }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220px">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleCancel(row)"
            v-if="row.status ===1&&row.status ===2">
            取消竞价
          </el-button>
          <el-button size="mini" @click="handleDetail(row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />

    <el-dialog title="详情" :visible.sync="dialogDetailVisible" width="888px" v-if="detail">
      <el-row :gutter="40">
        <el-col :span="8">竞价编号：<span style="font-size:13px">{{detail.identifier}}</span></el-col>
        <el-col :span="8">创建时间：{{detail.createTime}}</el-col>
        <el-col :span="8">订单状态：{{detail.statusText}}</el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8">起拍价：{{detail.topPrice}}</el-col>
        <el-col :span="16">竞价时间：{{detail.startDate}}至{{detail.endDate}}</el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8">最新价格：{{detail.newPrice}}</el-col>
        <el-col :span="8">出价次数：{{detail.priceTimes}}</el-col>
      </el-row>
      <el-card style="margin:20px 0" shadow="hover">
        <div slot="header" class="clearfix">
          <span>竞价明细</span>
        </div>
        <el-table :data="detail.details" style="width: 100%" border fit highlight-current-row row-key="id">
          <el-table-column label="企业名称" prop="companyName" align="center" />
          <el-table-column label="企业联系电话" prop="companyContactMobile" align="center" />
          <el-table-column label="竞拍价格" prop="price" align="center" />
          <el-table-column label="起拍价" prop="topPrice" align="center" />
          <el-table-column label="竞价时间" prop="createTime" align="center" />
        </el-table>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button @click="dialogDetailVisible = false">
          取消
        </el-button> -->
        <el-button type="primary" @click="dialogDetailVisible = false">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { biddingPageByQo, biddingGetById, biddingCancel } from '@/api/transaction'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { jcStatusJson } from '../constant'
import NumInput from '@/components/NumInput'
import DatePicker from '@/components/DatePicker'

export default {
  name: 'TransactionGroupBuying',
  directives: { waves },
  components: { Pagination, NumInput, DatePicker },
  filters: {
    statusFilter(status) { //状态
      const statusMap = {
        1: 'warning',
        2: '',
        3: 'success',
        98: 'info',
        99: 'danger',
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        status: '1',
        pageNo: 1,
        pageSize: 20,
        identifier: '',
      },
      jcStatusJson,
      dialogDetailVisible: false,
      detail: {}
    }
  },
  watch: {
    'listQuery.status': function () {
      this.handleFilter()
    },
  },
  created() {
    if (this.$route.query.status && this.listQuery.status !== this.$route.query.status.toString()) {
      this.listQuery.status = this.$route.query.status.toString()
    }
    if (this.$route.query.identifier) {
      this.listQuery.identifier = this.$route.query.identifier.toString();
    }

    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      biddingPageByQo(this.listQuery).then(response => {
        this.list = response.list || []
        this.total = response.totalCount
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleCreate() {
      // console.log(111)
      this.$router.push('/transaction/biddingBuyingSave')
    },
    handleDetail(row) {
      this.getDetail(row.id).then(res => {
        this.detail = Object.assign({}, res)
        this.dialogDetailVisible = true;
      })
    },
    getDetail(id) {
      return new Promise((resolve, reject) => {
        biddingGetById(id)
          .then(res => {
            resolve(res);
          })
          .catch(err => {
            reject(err);
          });
      });
    },
    handleCancel(row) {
      this.$confirm('是否取消竞价？').then(() => {
        biddingCancel({ id: row.id }).then(() => {
          // this.$notify({
          //   title: '成功',
          //   message: '操作成功！',
          //   type: 'success',
          //   duration: 2000
          // })

          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.getList()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row {
  line-height: 2;
  font-size: 15px;
}
</style>
